﻿#include "u_equipment_edit.h"

EquipmentEdit::EquipmentEdit(Equipment bean, QWidget *parent) : MyDialog(parent)
{
    this->bean = bean;

    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
    this->OnBtnResetClicked();

    this->setMaxHide();
    this->setTitle("发射机设置");
    this->resize(600, 350);
}

void EquipmentEdit::InitWidget(){
    btnEdit  = new QPushButton(QChar(0xe657) + QString(" 保存"));   btnEdit->setMinimumWidth(100);
    btnReset = new QPushButton(QChar(0xe6dc) + QString(" 重置"));   btnReset->setMinimumWidth(100);

    editSerialName = new QLineEdit();
    editdevCode = new QLineEdit(); editdevCode->setEnabled(this->bean.devCode.isEmpty());
    QStringList baud,data,check,stop;
    baud<<"1200"<<"2400"<<"4800"<<"9600"<<"14400"<<"19200"<<"38400"<<"56000"<<"57600"<<"115200"<<"194000";
    data<<"5"<<"6"<<"7"<<"8";
    check<<"N空"<<"E偶"<<"0奇"<<"S空格"<<"M标志";
    stop<<"1"<<"1.5"<<"2";
    editDevid = new QLineEdit();
    editDevName = new QLineEdit();
    editcosrs = new QLineEdit();
    editcosfs = new QLineEdit();
    editcosgp = new QLineEdit();
    editcos_140 = new QLineEdit();
    editcos_18 = new QLineEdit();
    editcos_12 = new QLineEdit();
    editcos_z18 = new QLineEdit();
    editcos_24 = new QLineEdit();
    editcosyp = new QLineEdit();
    editcoszf= new QLineEdit();
    editcostz = new QLineEdit();
    editcostd = new QLineEdit();
    editBaudRate = new QComboBox();
    editDataBit =new QComboBox();
    editCheckoutBit = new QComboBox();
    editStopBit = new QComboBox();
    editBaudRate->addItems(baud);
    editDataBit->addItems(data);
    editCheckoutBit->addItems(check);
    editStopBit->addItems(stop);
//    editTempThreshold = MyGUI::GetDoubleEdit(1, 100);
//    editHumThreshold = MyGUI::GetDoubleEdit(1, 100);
//    editTempTime = MyGUI::GetSpinBox(1, 600, "秒");
//    editHumTime = MyGUI::GetSpinBox(1, 600, "秒");
    //editEnable = new QCheckBox("启用");

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnEdit);
    btnLayout->addWidget(btnReset);

    QGridLayout *layout1 = new QGridLayout();
    layout1->addWidget(new QLabel("设备代号:"),   1,1,1,1, Qt::AlignRight);
    layout1->addWidget(editdevCode,              1,2,1,1);
    layout1->addWidget(new QLabel("设备名称:"),    1,3,1,1, Qt::AlignRight);
    layout1->addWidget(editDevName,               1,4,1,1);
    layout1->addWidget(new QLabel("设备号:"),   2,1,1,1, Qt::AlignRight);
    layout1->addWidget(editDevid,               2,2,1,1);

    layout1->addWidget(new QLabel("串口号:"),    2,3,1,1, Qt::AlignRight);
    layout1->addWidget(editSerialName,          2,4,1,1);
    layout1->addWidget(new QLabel("波特率:"),    3,1,1,1, Qt::AlignRight);
    layout1->addWidget(editBaudRate,            3,2,1,1);

    layout1->addWidget(new QLabel("数据位:"),   3,3,1,1, Qt::AlignRight);
    layout1->addWidget(editDataBit,            3,4,1,1);
    layout1->addWidget(new QLabel("校验位:"),   4,1,1,1, Qt::AlignRight);
    layout1->addWidget(editCheckoutBit,        4,2,1,1);
    layout1->addWidget(new QLabel("停止位:"),    4,3,1,1, Qt::AlignRight);
    layout1->addWidget(editStopBit,             4,4,1,1);

    layout1->addWidget(new QLabel("入射系数:"),   5,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcosrs,               5,2,1,1);
    layout1->addWidget(new QLabel("反射系数:"),   5,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcosfs,               5,4,1,1);
    layout1->addWidget(new QLabel("高频系数:"),   6,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcosgp,               6,2,1,1);
    layout1->addWidget(new QLabel("-140V系数:"),   6,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcos_140,               6,4,1,1);
    layout1->addWidget(new QLabel("-18V系数:"),   7,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcos_18,               7,2,1,1);
    layout1->addWidget(new QLabel("12V系数:"),   7,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcos_12,               7,4,1,1);
    layout1->addWidget(new QLabel("18V系数:"),   8,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcos_z18,               8,2,1,1);
    layout1->addWidget(new QLabel("24V系数:"),   8,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcos_24,               8,4,1,1);
    layout1->addWidget(new QLabel("音频系数:"),   9,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcosyp,               9,2,1,1);
    layout1->addWidget(new QLabel("中放系数:"),   9,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcoszf,               9,4,1,1);
    layout1->addWidget(new QLabel("调制系数:"),   10,1,1,1, Qt::AlignRight);
    layout1->addWidget(editcostz,               10,2,1,1);
    layout1->addWidget(new QLabel("推动系数:"),   10,3,1,1, Qt::AlignRight);
    layout1->addWidget(editcostd,               10,4,1,1);
    if(globalData->CheckUserRole(0)){
        layout1->addLayout(btnLayout,          11,1,1,4);
    }

    QGroupBox *groupbox1 = new QGroupBox();
    groupbox1->setStyleSheet("margin-top: 0px;");
    groupbox1->setLayout(layout1);

    //------------------------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(groupbox1);

    this->addLayout(mainLayout);

}

void EquipmentEdit::SetupConnections(){
    connect(btnEdit , SIGNAL(clicked()), this, SLOT(OnBtnEditClicked()));
    connect(btnReset , SIGNAL(clicked()), this, SLOT(OnBtnResetClicked()));
}

void EquipmentEdit::Initialize(){

}


void EquipmentEdit::OnBtnEditClicked(){
    if(this->editSerialName->text().trimmed().isEmpty()){ this->editSerialName->setFocus(); MyGUI::ShowMessageTip("请输入串口号", this); return; }
    if(this->editDevName->text().trimmed().isEmpty()){ this->editDevName->setFocus(); MyGUI::ShowMessageTip("请输入设备名称", this); return; }
    //if(this->edit->value() < 0){ this->editAddr->setFocus(); MyGUI::ShowMessageTip("请输入地址码", this); return; }

    if(!MyGUI::ShowMessageBoxQuesion("确定保存吗？", this)){
        return;
    }

    Equipment info = bean;
    info.cosrs = this->editcosrs->text().toFloat();
    info.devCode = this->editdevCode->text().trimmed();
    info.devname = this->editDevName->text().trimmed();
    info.serialName = this->editSerialName->text().trimmed();
    info.devid = this->editDevid->text().toUInt();
    info.baudRate = this->editBaudRate->currentText().toInt();
    info.dataBit = this->editDataBit->currentText().toInt();
    info.checkoutBit = this->editCheckoutBit->currentIndex();
    info.stopBit = this->editStopBit->currentIndex();
    info.cosfs = this->editcosfs->text().toFloat();
    info.cosgp = this->editcosgp->text().toFloat();
    info.cos_140 = this->editcos_140->text().toFloat();
    info.cos_18 = this->editcos_18->text().toFloat();
    info.cos_12 = this->editcos_12->text().toFloat();
    info.cos_z18 = this->editcos_z18->text().toFloat();
    info.cos_24 = this->editcos_24->text().toFloat();
    info.cosyp = this->editcosyp->text().toFloat();
    info.coszf = this->editcoszf->text().toFloat();
    info.costz = this->editcostz->text().toFloat();
    info.costd = this->editcostd->text().toFloat();
    //info.enable = this->editEnable->isChecked();

    bool b = false;

    if(globalData->sqlBase->GetEquipmentMap().contains(info.serialName)){
        b = globalData->sqlBase->UpdateEquipment(info);
        if(b){
        globalData->SaveSysLog("修改设备", 1);
        globalData->UpdateEquipment();
        }
    }else{
        b = globalData->sqlBase->SaveEquipment(info);
        if(b){
        globalData->SaveSysLog("添加设备", 1);
        }
    }

    if(b){
        this->close();
        MyGUI::ShowMessageBoxInfo("保存成功", this);
        globalData->InitEquipment();
        emit globalData->Update();
    }else{
        MyGUI::ShowMessageBoxError("保存失败", this);
    }
}

void EquipmentEdit::OnBtnResetClicked(){
    Equipment info = bean;

    editDevName->setText(info.devname);
    editSerialName->setText(info.serialName);
    editdevCode->setText(info.devCode);
    editcosrs->setText(QString::number(info.cosrs));
    editDevid->setText(QString::number(info.devid));
    editBaudRate->setCurrentText(QString::number(info.baudRate));
    editDataBit->setCurrentText(QString::number(info.dataBit));
    editCheckoutBit->setCurrentIndex(info.checkoutBit);
    editStopBit->setCurrentIndex(info.stopBit);
    editcosfs->setText(QString::number(info.cosfs));
    editcosgp->setText(QString::number(info.cosgp));
    editcos_140->setText(QString::number(info.cos_140));
    editcos_18->setText(QString::number(info.cos_18));
    editcos_12->setText(QString::number(info.cos_12));
    editcos_z18->setText(QString::number(info.cos_z18));
    editcos_24->setText(QString::number(info.cos_24));
    editcosyp->setText(QString::number(info.cosyp));
    editcoszf->setText(QString::number(info.coszf));
    editcostz->setText(QString::number(info.costz));
    editcostd->setText(QString::number(info.costd));
    //editEnable->setChecked(info.enable);
}
